package com.tlx.image;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.tlx.core.util.RandomUtils;
import com.tlx.core.util.TFileUtils;

//压缩图片工具
public class ImageQualityUtils {

    // 压缩一个目录下的图片，包括全部子目录
    public static void qualitySelfByDirIncludeSon(String dirPath, double quality) {

        // 先处理当前目录下图片
        qualitySelfByDir(dirPath, quality);

        // 再递归
        List<String> dirList = TFileUtils.getDirList(dirPath);
        for (String sonPath : dirList) {
            qualitySelfByDirIncludeSon(sonPath, quality);
        }

    }

    // 压缩一个目录下的图片，不包括子目录
    public static void qualitySelfByDir(String dir, double quality) {

        List<String> fileList = TFileUtils.getImageList(dir);

        if (CollectionUtils.isEmpty(fileList)) {
            return;
        }
        qualityList(fileList, quality);
    }

    // 将一组图片都缩放到一样的宽度，高度根据比例调整
    public static void qualityList(List<String> imgPathList, double quality) {
        for (String imgPath : imgPathList) {
            qualitySelf(imgPath, quality);
        }
    }

    // 修改图片质量，相当于缩放
    public static void qualitySelf(String srcPath, double quality) {
        quality(srcPath, srcPath, quality);
    }

    // 修改图片质量，相当于缩放
    public static String quality(String imgPath, String destPath, double quality) {
        if (StringUtils.isBlank(destPath)) {
            destPath = RandomUtils.randomName("quality", imgPath);
        }

        ImageBaseUtils.quality(imgPath, destPath, quality);

        return destPath;

    }
}
